﻿using Microsoft.Maui.ApplicationModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ymk_camera_surveillance.Models;

namespace ymk_camera_surveillance.CaptureCamera
{
    public class CapManager
    {
        public static CapManager Instance { get; } = new CapManager();

        private readonly Dictionary<string, CapRtsp> caps = new Dictionary<string, CapRtsp>();

        private CapRtsp AddCamera(string rtspUrl)
        {
            var cap = new CapRtsp();

            caps[rtspUrl] = cap;


            Thread t = new Thread(() =>
            {
                cap.StartCapture(rtspUrl);
            });
            t.Start();
            return cap;
        }

        public CapRtsp GetOrAddCapRtsp(string rtspUrl)
        {
            var cap = caps.GetValueOrDefault(rtspUrl, null);
            if (cap == null)
            {
                return AddCamera(rtspUrl);
            }
            return cap;
        }
    }
}
